package com.jrzh.security.authority;

import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
/**
 * 权限管理决断器
 * @author Xanthin
 *
 */
public class CustomAccessDecisionManager implements AccessDecisionManager {

	private static final Logger logger = Logger.getLogger(CustomAccessDecisionManager.class);
	
	@Override
	public void decide(Authentication authentication, Object object,Collection<ConfigAttribute> configAttributes) throws AccessDeniedException,InsufficientAuthenticationException {
		if(configAttributes == null){
			return;
		}
		//config urlroles
		Iterator<ConfigAttribute> iterator = configAttributes.iterator();
		while(iterator.hasNext()){
			ConfigAttribute configAttribute = iterator.next();
			//need role
			String needRole = configAttribute.getAttribute();
			//user roles
			for(GrantedAuthority ga : authentication.getAuthorities()){
				if(needRole.equals(ga.getAuthority())){
					return;
				}
			}
			logger.info("need role is " + needRole);
		}
		throw new AccessDeniedException("Cannot Access!");
	}

	@Override
	public boolean supports(ConfigAttribute arg0) {
		return true;
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}

}
